import { ipcRenderer } from 'electron'
import { FFmpegOptions } from './utils/ffmpeg'

// 定义IPC接口类型
export interface WindowControlAPI {
  minimize: () => void
  maximize: () => void
  close: () => void
}

export interface FileAPI {
  selectDirectory: () => Promise<{ success: boolean; path?: string; error?: string }>
  selectFile: (options: {
    accept: string
  }) => Promise<{ success: boolean; filePaths?: string[]; error?: string }>
  validateFiles: (options: {
    filePaths: string[]
    accept: string
  }) => Promise<{ success: boolean; filePaths?: string[]; error?: string }>
}

export interface DataAPI {
  getSystemInfo: () => Promise<{
    success: boolean
    data?: {
      platform: string
      arch: string
      nodeVersion: string
    }
    error?: string
  }>
}

export interface VideoAPI {
  convert: (options: FFmpegOptions | FFmpegOptions[]) => Promise<{
    success: boolean
    results?: { success: boolean; inputPath: string }[]
    error?: string
  }>
  cancelConvert: (inputPath: string) => Promise<{ success: boolean; error?: string }>
  onProgress: (
    listener: (progress: {
      percent: number
      frames: number
      currentFps: number
      currentKbps: number
      path: string
    }) => void
  ) => void
  onConversionComplete: (listener: (data: { path: string; success: boolean }) => void) => void
  removeListener: (channel: string, listener: (...args: any[]) => void) => void
  removeConversionCompleteListener: (
    listener: (data: { path: string; success: boolean }) => void
  ) => void
}

export interface IpcAPI {
  windowControl: WindowControlAPI
  file: FileAPI
  data: DataAPI
  video: VideoAPI
  on: (channel: string, listener: (...args: any[]) => void) => void
  once: (channel: string, listener: (...args: any[]) => void) => void
  removeAllListeners: (channel: string) => void
  removeListener: (channel: string, listener: (...args: any[]) => void) => void
}

/**
 * IPC渲染进程事件管理类
 * 用于集中处理所有从渲染进程发送到主进程的IPC请求
 */
class IpcRendererHandler implements IpcAPI {
  void: any
  /**
   * 窗口控制相关方法
   */
  public windowControl: WindowControlAPI = {
    /**
     * 最小化窗口
     */
    minimize: (): void => {
      ipcRenderer.send('window-control', 'minimize')
    },

    /**
     * 最大化/还原窗口
     */
    maximize: (): void => {
      ipcRenderer.send('window-control', 'maximize')
    },

    /**
     * 关闭窗口
     */
    close: (): void => {
      ipcRenderer.send('window-control', 'close')
    }
  }

  /**
   * 文件操作相关方法
   */
  public file: FileAPI = {
    /**
     * 选择文件夹
     * @returns 包含成功状态和所选路径的对象
     */
    selectDirectory: async (): Promise<{ success: boolean; path?: string; error?: string }> => {
      return await ipcRenderer.invoke('file:selectDirectory')
    },

    // 新增：选择文件
    selectFile: async (options: {
      accept: string
    }): Promise<{ success: boolean; filePaths?: string[]; error?: string }> => {
      return await ipcRenderer.invoke('file:selectFile', options)
    },

    validateFiles: async (options: {
      filePaths: string[]
      accept: string
    }): Promise<{ success: boolean; filePaths?: string[]; error?: string }> => {
      return await ipcRenderer.invoke('file:validateFiles', options)
    }
  }

  /**
   * 数据获取相关方法
   */
  public data: DataAPI = {
    /**
     * 获取系统信息
     * @returns 包含系统信息的对象
     */
    getSystemInfo: async (): Promise<{
      success: boolean
      data?: {
        platform: string
        arch: string
        nodeVersion: string
      }
      error?: string
    }> => {
      return await ipcRenderer.invoke('data:getSystemInfo')
    }
  }

  public video: VideoAPI = {
    /**
     * 转换视频
     * @param inputPath 输入文件路径
     * @param outputPath 输出文件路径
     * @returns 包含成功状态的对象
     */
    convert: async (
      options: FFmpegOptions | FFmpegOptions[]
    ): Promise<{
      success: boolean
      results?: { success: boolean; inputPath: string }[]
      error?: string
    }> => {
      return await ipcRenderer.invoke('video:convert', options)
    },
    cancelConvert: async (inputPath: string): Promise<{ success: boolean; error?: string }> => {
      return await ipcRenderer.invoke('video:cancelConvert', inputPath)
    },
    onProgress: (listener) => {
      ipcRenderer.on('video:progress', (event, progress) => listener(progress))
    },
    onConversionComplete: (listener) => {
      ipcRenderer.on('video:conversionComplete', (event, data) => listener(data))
    },
    removeListener: (channel: string, listener: (...args: any[]) => void): void => {
      ipcRenderer.removeListener(channel, listener)
    },
    removeConversionCompleteListener: (listener: (...args: any[]) => void): void => {
      ipcRenderer.removeListener('video:conversionComplete', listener)
    }
  }

  /**
   * 注册监听来自主进程的消息
   * @param channel 频道名称
   * @param listener 回调函数
   */
  public on(channel: string, listener: (...args: any[]) => void): void {
    ipcRenderer.on(channel, (event, ...args) => listener(...args))
  }

  /**
   * 注册一次性监听来自主进程的消息
   * @param channel 频道名称
   * @param listener 回调函数
   */
  public once(channel: string, listener: (...args: any[]) => void): void {
    ipcRenderer.once(channel, (event, ...args) => listener(...args))
  }

  /**
   * 移除特定频道的所有监听器
   * @param channel 频道名称
   */
  public removeAllListeners(channel: string): void {
    ipcRenderer.removeAllListeners(channel)
  }

  // 移除指定通道的监听器
  public removeListener(channel: string, listener: (...args: any[]) => void): void {
    // 调用ipcRenderer的removeListener方法，移除指定通道的监听器
    ipcRenderer.removeListener(channel, listener)
  }
}

// 导出单例实例
export const ipc = new IpcRendererHandler()
